package com.yw.swagger.controller;

import com.yw.swagger.customNote.fyh;
import com.yw.swagger.customNote.qw;
import com.yw.swagger.model.WebContents;
import com.yw.swagger.model.WebModels;
import com.yw.swagger.service.WebContentsService;
import com.yw.swagger.tool.AjaxResult;
import com.yw.swagger.tool.Utils.StringUtils;
import com.yw.swagger.tool.controller.BaseController;
import com.yw.swagger.tool.page.TableDataInfo;
import io.swagger.annotations.*;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Author: Abeiv
 * @Description:
 * @Date: 2019/10/26 11:06
 */
@Api(tags = "发布信息")
@Controller
@RequestMapping("/WebContents")
public class WebContentsController extends BaseController {

    @Autowired
    private WebContentsService webContentsService;

    @fyh
    @ApiOperation(value = "发布工作状态", notes = "WebContents")
    @ApiImplicitParam(name = "author", value = "权限码", required = true)
    @ApiResponses({@ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 500, message = "error")})
    @PostMapping("/addWork")
    @ResponseBody
    public AjaxResult addWork(WebContents WebContents) {
        if (StringUtils.isNull(WebContents.getAccount())) {
            return error("发布人不能为空");
        }
        if (StringUtils.isNull(WebContents.getContents())) {
            return error("内容不能为空");
        }
        if (StringUtils.isNull(WebContents.geteAccount())) {
            return error("发布人账号不能为空");
        }
        if (StringUtils.isNull(WebContents.getTitle())) {
            return error("标题不能为空");
        }
        return AjaxResult.success(webContentsService.insertContentsWork(WebContents));
    }

    @fyh
    @ApiOperation(value = "发布通知公告", notes = "WebContents")
    @ApiImplicitParam(name = "author", value = "权限码", required = true)
    @ApiResponses({@ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 500, message = "error")})
    @PostMapping("/addInform")
    @ResponseBody
    public AjaxResult addaddInform(WebContents WebContents) {
        if (StringUtils.isNull(WebContents.getAccount())) {
            return error("发布人不能为空");
        }
        if (StringUtils.isNull(WebContents.getContents())) {
            return error("内容不能为空");
        }
        if (StringUtils.isNull(WebContents.geteAccount())) {
            return error("发布人账号不能为空");
        }
        if (StringUtils.isNull(WebContents.getTitle())) {
            return error("标题不能为空");
        }
        return AjaxResult.success(webContentsService.insertContentsInform(WebContents));
    }

    @fyh
    @ApiOperation(value = "发布政务公开", notes = "WebContents")
    @ApiImplicitParam(name = "author", value = "权限码", required = true)
    @ApiResponses({@ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 500, message = "error")})
    @PostMapping("/addAffairs")
    @ResponseBody
    public AjaxResult addAffairs(WebContents WebContents) {
        if (StringUtils.isNull(WebContents.getAccount())) {
            return error("发布人不能为空");
        }
        if (StringUtils.isNull(WebContents.getContents())) {
            return error("内容不能为空");
        }
        if (StringUtils.isNull(WebContents.geteAccount())) {
            return error("发布人账号不能为空");
        }
        if (StringUtils.isNull(WebContents.getTitle())) {
            return error("标题不能为空");
        }
        return AjaxResult.success(webContentsService.insertContentsAffairs(WebContents));
    }

    @fyh
    @ApiOperation(value = "发布领导讲话", notes = "WebContents")
    @ApiImplicitParam(name = "author", value = "权限码", required = true)
    @ApiResponses({@ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 500, message = "error")})
    @PostMapping("/addSpeak")
    @ResponseBody
    public AjaxResult addSpeak(WebContents WebContents) {
        if (StringUtils.isNull(WebContents.getAccount())) {
            return error("发布人不能为空");
        }
        if (StringUtils.isNull(WebContents.getContents())) {
            return error("内容不能为空");
        }
        if (StringUtils.isNull(WebContents.geteAccount())) {
            return error("发布人账号不能为空");
        }
        if (StringUtils.isNull(WebContents.getTitle())) {
            return error("标题不能为空");
        }
        return AjaxResult.success(webContentsService.insertContentsSpeak(WebContents));
    }

    @fyh
    @ApiOperation(value = "发布专题栏目", notes = "WebContents")
    @ApiImplicitParam(name = "author", value = "权限码", required = true)
    @ApiResponses({@ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 500, message = "error")})
    @PostMapping("/addSpecial")
    @ResponseBody
    public AjaxResult addSpecial(WebContents WebContents) {
        if (StringUtils.isNull(WebContents.getAccount())) {
            return error("发布人不能为空");
        }
        if (StringUtils.isNull(WebContents.getContents())) {
            return error("内容不能为空");
        }
        if (StringUtils.isNull(WebContents.geteAccount())) {
            return error("发布人账号不能为空");
        }
        if (StringUtils.isNull(WebContents.getTitle())) {
            return error("标题不能为空");
        }
        return AjaxResult.success(webContentsService.insertContentsSpecial(WebContents));
    }

    @fyh
    @ApiOperation(value = "发布文件资料", notes = "WebContents")
    @ApiImplicitParam(name = "author", value = "权限码", required = true)
    @ApiResponses({@ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 500, message = "error")})
    @PostMapping("/addData")
    @ResponseBody
    public AjaxResult addData(WebContents WebContents) {
        if (StringUtils.isNull(WebContents.getAccount())) {
            return error("发布人不能为空");
        }
        if (StringUtils.isNull(WebContents.getContents())) {
            return error("内容不能为空");
        }
        if (StringUtils.isNull(WebContents.geteAccount())) {
            return error("发布人账号不能为空");
        }
        if (StringUtils.isNull(WebContents.getTitle())) {
            return error("标题不能为空");
        }
        return AjaxResult.success(webContentsService.insertContentsData(WebContents));
    }

    @fyh
    @ApiOperation(value = "发布规章制度", notes = "WebContents")
    @ApiImplicitParam(name = "author", value = "权限码", required = true)
    @ApiResponses({@ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 500, message = "error")})
    @PostMapping("/addSystem")
    @ResponseBody
    public AjaxResult addSystem(WebContents WebContents) {
        if (StringUtils.isNull(WebContents.getAccount())) {
            return error("发布人不能为空");
        }
        if (StringUtils.isNull(WebContents.getContents())) {
            return error("内容不能为空");
        }
        if (StringUtils.isNull(WebContents.geteAccount())) {
            return error("发布人账号不能为空");
        }
        if (StringUtils.isNull(WebContents.getTitle())) {
            return error("标题不能为空");
        }
        return AjaxResult.success(webContentsService.insertContentsSystem(WebContents));
    }

    @fyh
    @ApiOperation(value = "发布学习园地", notes = "WebContents")
    @ApiImplicitParam(name = "author", value = "权限码", required = true)
    @ApiResponses({@ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 500, message = "error")})
    @PostMapping("/addStudy")
    @ResponseBody
    public AjaxResult addStudy(WebContents WebContents) {
        if (StringUtils.isNull(WebContents.getAccount())) {
            return error("发布人不能为空");
        }
        if (StringUtils.isNull(WebContents.getContents())) {
            return error("内容不能为空");
        }
        if (StringUtils.isNull(WebContents.geteAccount())) {
            return error("发布人账号不能为空");
        }
        if (StringUtils.isNull(WebContents.getTitle())) {
            return error("标题不能为空");
        }
        return AjaxResult.success(webContentsService.insertContentsStudy(WebContents));
    }


    @fyh
    @ApiOperation(value = "发布公共信息", notes = "WebContents")
    @ApiImplicitParam(name = "author", value = "权限码", required = true)
    @ApiResponses({@ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 500, message = "error")})
    @PostMapping("/addPubilc")
    @ResponseBody
    public AjaxResult addPubilc(WebContents WebContents) {
        if (StringUtils.isNull(WebContents.getAccount())) {
            return error("发布人不能为空");
        }
        if (StringUtils.isNull(WebContents.getContents())) {
            return error("内容不能为空");
        }
        if (StringUtils.isNull(WebContents.geteAccount())) {
            return error("发布人账号不能为空");
        }
        if (StringUtils.isNull(WebContents.getTitle())) {
            return error("标题不能为空");
        }
        return AjaxResult.success(webContentsService.insertContentsPubilc(WebContents));
    }


    @fyh
    @ApiOperation(value = "分页查询某模块信息(无html)", notes = "分页查询某模块信息(无html)")
    @ApiResponses({@ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 500, message = "error")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "author", value = "权限码", required = true),
            @ApiImplicitParam(name = "model", value = "要查询模块", required = true),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "数据条数", required = true),
    })
    @PostMapping("/inquireAffairsNoCss")
    @ResponseBody
    public AjaxResult toUnreadNoCss(String model) {
        WebModels wm = webContentsService.seleWebModelByName(model);
        startPage();
        List<WebContents> l = webContentsService.seleCtcontentsAll(wm.getModuleID());
        TableDataInfo tdl = getDataTable(l);
        AjaxResult ar = new AjaxResult(AjaxResult.Type.SUCCESS, null);
        ar.put("rows", tdl.getRows());
        ar.put("total", tdl.getTotal());
        return ar;
    }

    @qw
    @ApiOperation(value = "分页查询某模块信息", notes = "分页查询某模块信息")
    @ApiResponses({@ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 500, message = "error")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "author", value = "权限码", required = true),
            @ApiImplicitParam(name = "model", value = "要查询模块", required = true),
    })
    @PostMapping("/inquireAffairs")
    @ResponseBody
    public AjaxResult toUnread(String model) {
        startPage();
        List<WebContents> l = webContentsService.seleCtcontentsAll(model);
        TableDataInfo tdl = getDataTable(l);
        AjaxResult ar = new AjaxResult(AjaxResult.Type.SUCCESS, null);
        ar.put("rows", tdl.getRows());
        ar.put("total", tdl.getTotal());
        return ar;
    }

    @fyh
    @ApiOperation(value = "查看信息详情", notes = "String")
    @ApiResponses({@ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 500, message = "error")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "author", value = "权限码", required = true),
            @ApiImplicitParam(name = "id", value = "id", required = true)
    })
    @PostMapping("/contentsSend")
    @ResponseBody
    public AjaxResult contentsSend(String id) throws DocumentException {
        //查询WebContents表详细信息
        WebContents webContents = webContentsService.seleCtcontentsSend(id);
        if (StringUtils.isNull(webContents)) {
            return error("没有数据");
        }

        String contents = "<div>" + webContents.getContents() + "</div>";
        contents = contents.replaceAll("<font", "<span");
        contents = contents.replaceAll("font>", "span>");
        webContents.setContents(contents);

        return AjaxResult.success(webContents);
    }

    @fyh
    @ApiOperation(value = "删除信息", notes = "String")
    @ApiResponses({@ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 500, message = "error")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "author", value = "权限码", required = true),
            @ApiImplicitParam(name = "id", value = "id", required = true)
    })
    @PostMapping("/contentsRemove")
    @ResponseBody
    public AjaxResult contentsRemove(String id) {
        //删除信息
        return AjaxResult.success(webContentsService.deleteContents(id));
    }

    @fyh
    @ApiOperation(value = "审核信息", notes = "String")
    @ApiResponses({@ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 500, message = "error")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "author", value = "权限码", required = true),
            @ApiImplicitParam(name = "id", value = "id", required = true)
    })
    @PostMapping("/contentsAudit")
    @ResponseBody
    public AjaxResult contentsAudit(String id) {
        startPage();
        //审核信息
        return AjaxResult.success(webContentsService.contentsAudit(id));
    }

    @fyh
    @ApiOperation(value = "未审核信息", notes = "String")
    @ApiResponses({@ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 500, message = "error")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "author", value = "权限码", required = true),
    })
    @PostMapping("/notAudit")
    @ResponseBody
    public AjaxResult notAudit() {
        startPage();
        //未审核信息
        return AjaxResult.success(webContentsService.selectNotAudit());
    }

    public void getNodes(Element node) {
        List<Element> listel = node.elements();
        for (Element e : listel) {
            if (e.getName().equals("font")) {
                e.setName("span");
                System.err.println(1);
            }
            getNodes(e);
        }
    }

    @fyh
    @ApiOperation(value = "查询新闻图片", notes = "查询新闻图片(无html)")
    @ApiResponses({@ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 500, message = "error")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "author", value = "权限码", required = true),
    })
    @PostMapping("/imgUrl")
    @ResponseBody
    public AjaxResult imgUrl() {
        //查询5张图片
        List<WebContents> webContents = webContentsService.selectImgUrl();
        TableDataInfo tdl = getDataTable(webContents);
        AjaxResult ar = new AjaxResult(AjaxResult.Type.SUCCESS, null);
        ar.put("rows", tdl.getRows());
        ar.put("total", tdl.getTotal());
        return ar;
    }


}
